#encoding: utf-8 
require 'rubygems'
require 'nokogiri'
require 'open-uri'

SERVER = 'http://all.wasu.cn'

namespace :db do
  namespace :episodes do
    task :load => :environment do
      load_episodes
    end

    task :reset => :environment do
      Video.delete_all :kind => 'episode'
      Category.delete_all :kind => 'episode'
    end
    
    def load_episodes
      puts "load episodes......"
      index = "http://all.wasu.cn/index/cid/11"
      {
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=1" => "文艺情感",
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=2" => "文艺情感", 
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=3" => "文艺情感",  
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=4" => "文艺情感",
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=5" => "文艺情感", 
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=6" => "文艺情感", 
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=7" => "文艺情感", 
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=8" => "文艺情感", 
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=11" => "文艺情感", 
        "http://all.wasu.cn/index/sort/time/cid/14/class/program?&p=12" => "文艺情感", 

   
        "http://all.wasu.cn/index/sort/time/cid/12/class/program?&p=1" => "人文军旅",  
        "http://all.wasu.cn/index/sort/time/cid/12/class/program?&p=2" => "人文军旅",  
        "http://all.wasu.cn/index/sort/time/cid/12/class/program?&p=3" => "人文军旅",  
        "http://all.wasu.cn/index/sort/time/cid/12/class/program?&p=4" => "人文军旅",
        "http://all.wasu.cn/index/sort/time/cid/12/class/program?&p=5" => "人文军旅",


        "http://all.wasu.cn/index/sort/time/cid/16/class/program?&p=1" => "刑侦悬疑",  
        "http://all.wasu.cn/index/sort/time/cid/16/class/program?&p=2" => "刑侦悬疑",  

        "http://all.wasu.cn/index/sort/time/cid/13/class/program?&p=1" => "言情偶像",
        "http://all.wasu.cn/index/sort/time/cid/13/class/program?&p=2" => "言情偶像",
        "http://all.wasu.cn/index/sort/time/cid/13/class/program?&p=3" => "言情偶像",


        "http://all.wasu.cn/index/sort/time/cid/15/class/program?&p=2" => "历史古装",
        "http://all.wasu.cn/index/sort/time/cid/15/class/program?&p=1" => "历史古装",        
        "http://all.wasu.cn/index/sort/time/cid/15/class/program?&p=3" => "历史古装",

        "http://all.wasu.cn/index/sort/time/cid/15/class/program?&p=4" => "经典动作",
        "http://all.wasu.cn/index/sort/time/cid/15/class/program?&p=5" => "经典动作",
        "http://all.wasu.cn/index/sort/time/cid/15/class/program?&p=6" => "经典动作",
        
        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=1" => "卡通动漫",
        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=2" => "卡通动漫",
        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=3" => "卡通动漫",
        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=4" => "卡通动漫",
        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=5" => "卡通动漫",
        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=6" => "卡通动漫",


        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=7" => "儿童影视",
        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=8" => "儿童影视",
        "http://all.wasu.cn/index/sort/time/cid/18/class/program?&p=9" => "儿童影视",

        "http://all.wasu.cn/index/sort/time/cid/17/class/program?&p=1" => "武侠魔幻",
        "http://all.wasu.cn/index/sort/time/cid/17/class/program?&p=2" => "武侠魔幻",
        "http://all.wasu.cn/index/sort/time/cid/17/class/program?&p=3" => "武侠魔幻",
 
      }.each do |category_link, category_name|
        begin
          #打开电视剧分类页面
          puts "load #{category_name} ... \n"
          category_page = open_link(category_link)
          category = Category.find_or_create_by_name_and_kind(category_name, 'episode')
          category_page.css('#publish dl').reverse.each do |dl|
            begin 
              episode_name = dl.at('dd p.item_title a').content
              episode_link = dl.css('dt a').first
              #打开电视剧页面
              puts "episode url #{episode_link['href']}"
              episode_page = open_link(episode_link['href'])
              
              video = Video.find_by_origin_url episode_link['href']

              if video
                video.update_attributes :category => category, :published => true
              else             
                image = episode_page.at('.teleplay_img img').attributes['src'].value
                actors = episode_page.css('.tele_info .tele_l p').last.css('a').map{|a|a.content}

                info = episode_page.css('.teleplay_tit p').last.at('#infoL').content.split("：").last.to_s
                video = Video.find_or_create_by_name episode_name
                video.generate_cover(image)
                video.update_attributes :kind => 'episode', :category => category, :body => info, :has_clip => true, 
                  :link => episode_link, :origin_url => episode_link['href']
                actors.each do |actor_name|
                  actor = Actor.find_or_create_by_name actor_name
                  video.actors << actor
                end
                video.save
                episode_page.css('.teleplay_gather ul.gather li a').each_with_index do |clip_link, index|
                  begin
                    clip_page = open_link(clip_link['href'])
                    clip_name = clip_page.css('.movie_l h3 span').first.content
                    clip = Clip.find_or_create_by_name_and_video_id(clip_name, video.id)
                    clip_link = clip_page.at('a.f_download')['onclick'].split("'")[3].strip
                    puts "clip link is #{clip_link}"
									 clip.update_attributes :seq => index, :link => clip_link, :kind => 'episode'
                    puts "\n load #{clip.name}"
                  rescue
                    puts "broken link "
                  end
                end
              end
              puts "\n####### load video: #{video.name},  clips #{video.clips.size}######"
            rescue
              puts "broken link "
            end
          end
        end
      end
    end

    def open_link(link)
      puts "open link #{link}..."
      Nokogiri::HTML(open(link))
    end

    def load_page(link)
      puts "open #{SERVER+link} ..."
      Nokogiri::HTML(open("#{SERVER+link}"))
    end
  end
end
